package br.ykhan.t5.ytools.menu.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import br.ykhan.t5.ytools.menu.beans.SiteMenu;
import br.ykhan.t5.ytools.menu.beans.SitePage;

/**
 * @see Utils
 */
public class UtilsTester
{
	/**
	 * @see Utils#occurrencesOf(String, char)
	 */
	@Test
	public void testOccurencesOf()
	{
		assertEquals(1, Utils.occurrencesOf("user/edit", "/"));
		assertEquals(0, Utils.occurrencesOf("", "/"));
	}
	
	/**
	 * @see Utils#getMenuPath(String)
	 */
	@Test
	public void testGetMenuPath()
	{
		assertEquals("", Utils.getMenuPath("index"));
		assertEquals("management", Utils.getMenuPath("management/general"));
		assertEquals("management/user", Utils.getMenuPath("management/user/edit"));
	}
	
	/**
	 * @see Utils#getPagePath(String)
	 */
	@Test
	public void testIsEmptyMenu()
	{
		SiteMenu emptyMenu = new SiteMenu();
		
		SiteMenu menuWithSubmenus = new SiteMenu();
		menuWithSubmenus.getSubMenus().add(new SiteMenu());
		
		SiteMenu menuWithPages = new SiteMenu();
		menuWithPages.getPages().add(new SitePage("management/general", String.class));
		
		SiteMenu menuWithSubmenusAndPages = new SiteMenu();
		menuWithSubmenusAndPages.getSubMenus().add(new SiteMenu());
		menuWithSubmenusAndPages.getPages().add(new SitePage("management/general", String.class));
		
		assertTrue(Utils.isEmptyMenu(emptyMenu));
		assertFalse(Utils.isEmptyMenu(menuWithSubmenus));
		assertFalse(Utils.isEmptyMenu(menuWithPages));
		assertFalse(Utils.isEmptyMenu(menuWithSubmenusAndPages));
	}
	
	@Test
	public void testSortPageNamesByLevel()
	{
		List<String> pageNames = new ArrayList<String>();
		pageNames.add("management/general");
		pageNames.add("management/user/list");
		pageNames.add("index");
		pageNames.add("management/user/edit");
		
		Utils.sortPageNamesByLevel(pageNames);
		
		assertEquals("index", pageNames.get(0));
		assertEquals("management/general", pageNames.get(1));
		assertEquals("management/user/edit", pageNames.get(2));
		assertEquals("management/user/list", pageNames.get(3));
	}
}
